package vn.vfriends.vbox.service.impl;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vbox.model.Role;
import vn.vfriends.vbox.model.RoleCode;
import vn.vfriends.vbox.rest.ApiConfig;
import vn.vfriends.vbox.rest.Authenticator;
import vn.vfriends.vbox.service.RoleService;

/**
 *
 * @author tuancn
 */
@Stateless
public class RoleServiceImpl implements RoleService {

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
    
    private Client client;
    
    @Inject @ApiConfig("API_URL")
    private String apiUrl;
    
    @Inject @ApiConfig("API_USER")
    private String apiUser;
    
    @Inject @ApiConfig("API_PASSWORD")
    private String apiPassword;
    
    @PostConstruct
    private void init() {
        client = ClientBuilder.newClient().register(new Authenticator(apiUser, apiPassword));
    }

    public RoleServiceImpl() {
    }
    
    public RoleServiceImpl(String apiUrl, String apiUser, String apiPassword) {
        this.apiUrl = apiUrl;
        this.apiUser = apiUser;
        this.apiPassword = apiPassword;
        client = ClientBuilder.newClient().register(new Authenticator(apiUser, apiPassword));
    }
    
    @Override
    public List<Role> findAll() {
        WebTarget target = client.target(apiUrl + "/roles");
        return target.request().get(new GenericType<List<Role>>() {});
    }

    @Override
    public Role create(Role role) {
        WebTarget target = client.target(apiUrl + "/roles");
        
        Entity<Role> entity = Entity.entity(role, MediaType.APPLICATION_XML);
        return target.request().post(entity, Role.class);
    }

    @Override
    public Role find(Integer id) {
        WebTarget target = client.target(apiUrl + "/roles").path(id.toString());
        return target.request().get(Role.class);
    }
    
    @Override
    public Role findByCode(RoleCode code) {
        WebTarget target = client.target(apiUrl + "/roles")
                .path("code/" + code.toString());
        return target.request().get(Role.class);
    }
    
    @Override
    public Role findByName(String name) {
        WebTarget target = client.target(apiUrl + "/roles")
                .path("name/" + name);
        return target.request().get(Role.class);
    }

    @Override
    public Role edit(Role role) {
        WebTarget target = client.target(apiUrl + "/roles");
        
        Entity<Role> entity = Entity.entity(role, MediaType.APPLICATION_XML);
        return target.request().put(entity, Role.class);
    }

    @Override
    public void remove(Role role) {
        WebTarget target = client.target(apiUrl + "/roles")
                .path(role.getId().toString());
        target.request().delete();
    }
}
